//
//  HTDeliverdCell.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/16.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTDeliverdCell.h"

@interface HTDeliverdCell()
@property (weak, nonatomic) IBOutlet UILabel *startLabel;
@property (weak, nonatomic) IBOutlet UILabel *destiLabel;
@property (weak, nonatomic) IBOutlet UILabel *detialLabel;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *lookLabel;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet UIButton *firstButton;
@property (weak, nonatomic) IBOutlet UIButton *secondButton;
@property (weak, nonatomic) IBOutlet UIButton *thirdButton;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (nonatomic,assign) BOOL flag;

@end

@implementation HTDeliverdCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.flag = NO;
}
- (void)setHolderGoodsModel:(HTHolderGoodsModel *)holderGoodsModel{
    _holderGoodsModel = holderGoodsModel;
    self.flag = YES;
    self.startLabel.text = holderGoodsModel.from;
    self.destiLabel.text = holderGoodsModel.to;
    self.lookLabel.text = [NSString stringWithFormat:@"%@人次查看 %@人次联系",holderGoodsModel.seenumber,holderGoodsModel.callnumber];
    NSString *length = [HTUtils sortLengthDisplayStringWith:holderGoodsModel.lenght];
    NSString *type = @"不限车型";
    if (!(holderGoodsModel.lengthtype == nil || holderGoodsModel.lengthtype.length <= 0)) {
        type = holderGoodsModel.lengthtype;
    }
    NSString *dun = @"";
    if ([holderGoodsModel.unit isEqualToString:@"1"]) {
        dun =[NSString stringWithFormat:@"%@吨",holderGoodsModel.weight];
    }else{
        dun =[NSString stringWithFormat:@"%@方",holderGoodsModel.weight];
    }
    NSString *cartype = @"";
    if (!(holderGoodsModel.cargotype == nil || holderGoodsModel.cargotype.length <= 0)) {
        cartype = holderGoodsModel.cargotype;
    }
   self.detialLabel.text = [NSString stringWithFormat:@"%@ %@ / %@ %@ / 运费%@元",length,type,dun,cartype,holderGoodsModel.price];
    self.timeLabel.text =[HTUtils timeIntervalExchange:holderGoodsModel.createtime];
    self.firstButton.hidden = YES;
    self.secondButton.hidden = YES;
    self.lineView.hidden = YES;
    [self.thirdButton setTitle:@"联系货主" forState:UIControlStateNormal];
    
}
- (void)setDeliverGoodsModel:(HTDeliverGoodsModel *)deliverGoodsModel{
    _deliverGoodsModel = deliverGoodsModel;
    self.flag = NO;
    self.startLabel.text = deliverGoodsModel.fromadd;
    self.destiLabel.text = deliverGoodsModel.toadd;
    self.lookLabel.text = [NSString stringWithFormat:@"%@人次查看 %@人次联系",deliverGoodsModel.viewnumber,deliverGoodsModel.callnumber];
    NSString *length = [HTUtils sortLengthDisplayStringWith:deliverGoodsModel.heiht];
    NSString *type = @"不限车型";
    if (!(deliverGoodsModel.type == nil || deliverGoodsModel.type.length <= 0)) {
        type = deliverGoodsModel.type;
    }
    NSString *dun = @"";
    if ([deliverGoodsModel.utit isEqualToString:@"1"]) {
        dun =[NSString stringWithFormat:@"%@吨",deliverGoodsModel.weight];
    }else{
        dun =[NSString stringWithFormat:@"%@方",deliverGoodsModel.weight];
    }
    NSString *cartype = @"";
    if (!(deliverGoodsModel.cartype == nil || deliverGoodsModel.cartype.length <= 0)) {
        cartype = deliverGoodsModel.cartype;
    }
    self.detialLabel.text = [NSString stringWithFormat:@"%@ %@ / %@ %@ / 运费%@元",length,type,dun,cartype,deliverGoodsModel.pricers];
    self.timeLabel.text = [HTUtils timeIntervalExchange:deliverGoodsModel.createtime];
}
- (void)setIsRuning:(BOOL)isRuning{
    _isRuning = isRuning;
    if (isRuning) {
        self.firstButton.hidden = YES;
        self.lineView.hidden = YES;
        [self.thirdButton setTitle:@"关闭" forState:UIControlStateNormal];
    }else{
        self.firstButton.hidden = YES;
        self.lineView.hidden = YES;
        [self.thirdButton setTitle:@"删除" forState:UIControlStateNormal];
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}
#pragma mark --- 网络请求

- (IBAction)didClickFirstButton:(id)sender {
    if (self.isRuning) {
        //MARK:发到微信
        
    }
}
- (IBAction)didClickSecondButton:(id)sender {
    //重发
    [self netLoadDataWithState:@"0"];
    
}
- (IBAction)didClickThirdButton:(id)sender {
    if (self.flag) {
        if (self.holderGoodsModel.phone == nil || self.holderGoodsModel.phone.length <= 0) {
            return;
        }
        NSString *phone = self.holderGoodsModel.phone;
        NSString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        return;
    }
    if (self.isRuning) {
        //关闭
        [self netLoadDataWithState:@"1"];
    }else{
        [UIAlertController alertStyleShowInController:self.controller title:@"温馨提示" message:@"你确定删除该货源消息吗？" alertActionsBlock:^(NSMutableArray<UIAlertAction *> *alertActions) {
            [alertActions addObject:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //删除
                [self netLoadDataWithState:@"2"];
            }]];
            [alertActions addObject:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        } completion:nil];
        
    }
}
//   0 重发 ，1 关闭  2 删除
- (void)netLoadDataWithState:(NSString *)state{
    YDHUD *hud = [YDHUD juhuaHUD];
    [self.controller.view addSubview:hud];
    [hud showAnimated:YES];
    [HTHttpRequestManager requestDeliverStateChangeWithProduceid:self.deliverGoodsModel.orderid state:state success:^(id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        DBLog(@"货源状态更改：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"货源状态变更失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        if (self.refreshBlock) {
            self.refreshBlock();
        }
    } failure:^(NSError * _Nonnull error) {
        [hud hideAnimated:YES];
        
    }];
}
@end
